package com.hqyj.module.holiday.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.module.clock.pojo.SearchBean;
import com.hqyj.module.holiday.dao.MyHolidayDao;
import com.hqyj.module.holiday.pojo.Holiday;
import com.hqyj.module.holiday.pojo.MyUserInfo;
import com.hqyj.module.holiday.service.HolidayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lap
 * @version 1.0
 * @date 2021/10/8 10:43
 */
@Service
public class HolidayServiceImpl implements HolidayService {

    @Autowired
    MyHolidayDao holidayDao;

    @Override
    public Map<String, Object> insertHoliday(Holiday holiday) {
        Map<String,Object> map = new HashMap<>();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date data = new Date(holiday.getStartTime());
        Date data1 = new Date(holiday.getEndTime());
        String format = sf.format(data.getTime());
        holiday.setStartTime(format);
        String format1 = sf.format(data1.getTime());
        holiday.setEndTime(format1);
        Integer integer = holidayDao.insertHoliday(holiday);
        if(integer>0){
            map.put("info","操作成功");
        }else {
            map.put("info","操作失败");
        }
        return map;
    }

    @Override
    public PageInfo<Holiday> getCitiesBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();

        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(holidayDao.getCitiesBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public PageInfo<Holiday> getBusinessBySearchBean(SearchBean searchBean) {
        searchBean.initSearchBean();

        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(holidayDao.getBusinessBySearchBean(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public PageInfo<Holiday> getBusinessBySearchBeanApp(SearchBean searchBean) {
        searchBean.initSearchBean();

        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(holidayDao.getBusinessBySearchBeanApp(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public PageInfo<Holiday> getThingBySearchBeanApp(SearchBean searchBean) {
        searchBean.initSearchBean();
        PageHelper.startPage(searchBean.getCurrentPage(), searchBean.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(holidayDao.getThingBySearchBeanApp(searchBean))
                .orElse(Collections.emptyList()));
    }

    @Override
    public List<Holiday> getHolidays(Integer userId) {
        char c = String.valueOf(userId).charAt(0);
        String likeId = c+"%";
        List<Holiday> holidays = holidayDao.getHolidays(likeId);
        return holidays;
    }

    @Override
    public Map<String, Object> getUserInfo(Integer userId) {
        MyUserInfo userInfo = holidayDao.getUserInfo(userId);
        Map<String,Object> map = new HashMap<>();
        if(userInfo.getRole().equals("经理")){
            map.put("info","经理");
        }else {
            map.put("info","员工");
        }
        return map;
    }
}
